<?php
if (!$is_success) {
    $dp = new jDatePicker();
    $dp->__dateFormat = "yy-mm-dd";
    echo "<scr" . "ipt type=\"text/javascript\">";
    $dp->generateDP("tanggal_invoice");
    echo "</scr" . "ipt>";
    $dp->init();
    ?>
<script>

    function updateGrandTotal() {
        if (isUsePPN()) {
            jQuery("#ppntotal").html(parseFloat(jQuery("#total").html()) * 0.0120);
            jQuery("#ppntotald").html(parseFloat(jQuery("#totald").html()) * 0.0120);
        } else {
            jQuery("#ppntotal").html(0);
            jQuery("#ppntotald").html(0);
        }
        jQuery("#grandtotal").html(parseFloat(jQuery("#total").html()) + parseFloat(jQuery("#ppntotal").html()));
        jQuery("#grandtotald").html(parseFloat(jQuery("#totald").html()) + parseFloat(jQuery("#ppntotald").html()));

        ReFormat();
    }

    function updateCrcSymbol() {
        var symbol = jQuery("#curency").val();
        if (symbol == "IDR") {
            jQuery(".IDR").show();
            jQuery(".USD").hide();
        } else {
            jQuery(".IDR").hide();
            jQuery(".USD").show();
        }
    }

    function isUsePPN() {
        var use = document.getElementById("tambahkan_ppn").checked;
        if (use) {
            return true;
        } else {
            return false;
        }
    }

    function setBlnToRome(bln) {
        if (bln == "01") {
            return "I";
        } else if (bln == "02") {
            return "II";
        } else if (bln == "03") {
            return "III";
        } else if (bln == "04") {
            return "IV";
        } else if (bln == "05") {
            return "V";
        } else if (bln == "06") {
            return "VI";
        } else if (bln == "07") {
            return "VII";
        } else if (bln == "08") {
            return "VIII";
        } else if (bln == "09") {
            return "IX";
        } else if (bln == "10") {
            return "X";
        } else if (bln == "11") {
            return "XI";
        } else {
            return "XII";
        }
    }

    function setInvoiceNumber() {
        var prefix = <?php echo json_encode($prefix); ?>;

        var date = (jQuery("#tanggal_invoice").val()).toString();
        var partsArray = date.split('-');
        var tgl = partsArray[2];
        var bln = setBlnToRome(partsArray[1]);
        var thn = partsArray[0];
        var no = prefix + "/ASP/INV-TC/" + tgl + "/" + bln + "/" + thn;
        jQuery("#nomor_invoice").val(no);

    }

    function close_gb() {
    <?php echo CloseGB(false); // close greybox     ?>
    }

    function _obj_fSendValue(kode) {
        location.href = '<?php echo site_url("transaksi_penyewaan_tc/invoice_add/$kode") ?>?id_perjanjian=' + kode;
    }

    function formatNumber(obj, decimal) {
        //decimal  - the number of decimals after the digit from 0 to 3
        //-- Returns the passed number as a string in the xxx,xxx.xx format.
        anynum = eval(obj);
        divider = 10;
        switch (decimal) {
            case 0:
                divider = 1;
                break;
            case 1:
                divider = 10;
                break;
            case 2:
                divider = 100;
                break;
            default:       //for 3 decimal places
                divider = 1000;
        }

        workNum = Math.abs((Math.round(anynum * divider) / divider));
        workStr = "" + workNum
        if (workStr.indexOf(",") == -1) {
            workStr += ","
        }
        dStr = workStr.substr(0, workStr.indexOf(","));
        dNum = dStr - 0
        pStr = workStr.substr(workStr.indexOf(","))

        while (pStr.length - 1 < decimal) {
            pStr += "0"
        }

        if (pStr == ',')
            pStr = '';
        //--- Adds a comma in the thousands place.
        if (dNum >= 1000) {
            dLen = dStr.length
            dStr = parseInt("" + (dNum / 1000)) + "." + dStr.substring(dLen - 3, dLen)
        }
        //-- Adds a comma in the millions place.
        if (dNum >= 1000000) {
            dLen = dStr.length
            dStr = parseInt("" + (dNum / 1000000)) + "." + dStr.substring(dLen - 7, dLen)
        }

        retval = dStr + pStr
        //-- Put numbers in parentheses if negative.
        if (anynum < 0) {
            retval = "(" + retval + ")";
        }
        //You could include a dollar sign in the return value.
        //retval =  "$"+retval
        return retval;
    }

    function ReFormat() {
        try {
            jQuery(".shou").each(function() {
                jQuery("#" + this.id + "_show").html(formatNumber(parseFloat(jQuery(this).html()), 2));
            });
        } catch (e) {
            alert(e);
        }
    }

    function delete_dn(kode) {
        if (confirm("Apakah anda yakin akan menghapus data ini?"))
            location.href = '<?php echo base_url() . "index.php/transaksi/delivery_delete/" ?>' + kode + '/<?php echo $kode ?>';
    }



    function append_table(result) {
        //var result = jQuery.parseJSON(array);
        var length = jQuery("#tablebody").find("tr").length;

        if (length == 1) {
            jQuery("#no_item").hide();
        }
        var id = length + '' + result.item;
        jQuery("#tablebody").append(
        '<tr class="GridItem1">' +
            '<td>' + length + '<input type="hidden" name="id_item[]" value="' + result.item + '" /></td>' +
            '<td><input name="nama_item[]" type=text value="' + result.nama + '" id=nama_item_' + id + ' ></td>' +
            '<td><input name="quantity[]" onblur="UpdateTotals(' + id + ');" type=text value=' + result.qty + ' id=qty_' + id + '> ' + result.satuan + '</td>' +
            '<td class="IDR" align="right"><input name="harga_beli[]" onblur="UpdateTotals(' + id + ');" type=text value=' + result.harga_beli + ' id=harga_beli_' + id + '></td>' +
            '<td class="IDR" align="right"><input name="harga_jual[]" onblur="UpdateTotals(' + id + ');" type=text value=' + result.harga_jual + ' id=harga_jual_' + id + '></td>' +
            '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=total id=total_' + id + '>' + result.total + '</span></td>' +
            '<td class="USD" align="right"><input name="harga_beli_dollar[]" onblur="UpdateTotals(' + id + ');" type=text value=' + result.harga_beli_dollar + ' id=harga_beli_dollar_' + id + '></td>' +
            '<td class="USD" align="right"><input name="harga_jual_dollar[]" onblur="UpdateTotals(' + id + ');" type=text value=' + result.harga_jual_dollar + ' id=harga_jual_dollar_' + id + '></td>' +
            '<td class="USD" align="right"><span style="float: left;"></span><span class=total_dollar id=total_dollar_' + id + '>' + result.total_dollar + '</span></td>' +
            '<td><a class="delete" onclick="remove_element(' + id + ',this);">&nbsp;</a></td>' +
            '</tr>'
    );

        jQuery("#total").html(parseFloat(jQuery("#total").html()) + parseFloat(result.total));
        jQuery("#totald").html(parseFloat(jQuery("#totald").html()) + parseFloat(result.total_dollar));

        CalculateTotal();
        updateCrcSymbol();

    }


    function UpdateTotals(id) {
        var qty = jQuery("#qty_" + id).val();
        var harga_jual = jQuery("#harga_jual_" + id).val();
        jQuery("#total_" + id).html(parseInt(qty) * parseFloat(harga_jual));

        var harga_jual_dollar = jQuery("#harga_jual_dollar_" + id).val();
        jQuery("#total_dollar_" + id).html(parseInt(qty) * parseFloat(harga_jual_dollar));

        CalculateTotal();
    }

    function CalculateTotal() {
        var totals = jQuery('.total');
        var grandTotal = 0;
        jQuery.each(totals, function(i) {
            grandTotal += parseFloat(jQuery(totals[i]).html());
        });
        jQuery('#total').html(parseFloat(grandTotal));

        var totals = jQuery('.total_dollar');
        var grandTotal = 0;
        jQuery.each(totals, function(i) {
            grandTotal += parseFloat(jQuery(totals[i]).html());
        });
        jQuery('#totald').html(parseFloat(grandTotal));

        updateGrandTotal();
        ReFormat();
    }

    function remove_element(id, elm) {
        var total_deleted = jQuery('#total_' + id).html();
        jQuery("#total").html(parseFloat(jQuery("#total").html()) - parseFloat(total_deleted));
        var tr_parent = jQuery(elm).parent().parent();
        jQuery(tr_parent).remove();


        var total_deleted = jQuery('#total_dollar_' + id).html();
        jQuery("#totald").html(parseFloat(jQuery("#totald").html()) - parseFloat(total_deleted));
        var tr_parent = jQuery(elm).parent().parent();
        jQuery(tr_parent).remove();

        var length = jQuery("#tablebody").find("tr").length;

        if (length == 1) {
            jQuery("#no_item").show();
        }
        CalculateTotal();
        ReFormat();
    }

    function clear_element(elm) {
        jQuery("#nama_" + elm).removeClass("read-only");
        jQuery("#nama_" + elm).attr("disabled", false);
        jQuery("#nama_" + elm).val("");
        jQuery("#id_" + elm).val("");
    }

</script>

<style>
    .shou{
        display: none;
    }
</style>

<h3 class="title-popup">Form Tambah Invoice</h3>

<div style="padding:0 10px 0 10px;">
        <?php if ($error != "") { ?>
    <div class="BoxMessage Warning">Proses Gagal, <br /><?php echo $error; ?></div>
            <?php } ?>

    <div id="content">
        <div class="DivTable">
                <?= form_open(site_url("transaksi_penyewaan_tc/invoice_add/$kode?po=$id_perjanjian")) ?>
            <table>
                <tr>
                    <td width="5%">&nbsp;</td>
                    <td width="1%">&nbsp;</td>
                    <td width="35%">&nbsp;</td>
                    <td width="5%">&nbsp;</td>
                    <td width="8%">&nbsp;</td>
                    <td width="5%">&nbsp;</td>
                    <td width="1%">&nbsp;</td>
                    <td width="35%">&nbsp;</td>
                    <td width="5%">&nbsp;</td>
                </tr>
                <tr>
                    <td>Perjanjian NO</td>
                    <td>:</td>
                    <td>
                        <input type="text" name="nomor_perjanjian" id="nomor_perjanjian" value="<?php echo $nomor_perjanjian; ?>" maxlength="40" size="50" readonly='readonly' class='read-only'/>
                        <input type="hidden" name="id_perjanjian" id="id_perjanjian" value="<?php echo $id_perjanjian; ?>"   />
                    </td>
                    <td><?php echo $gbpo; ?></td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                    <td>Date</td>
                    <td>:</td>
                    <td><?php echo $tanggal_perjanjian; ?></td>
                </tr>
                <tr>
                    <td>INV No</td>
                    <td>:</td>
                    <td><?php echo form_input(array("name" => "nomor_invoice", "id" => "nomor_invoice", "size" => "50"), $nomor_invoice); ?></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                    <td>Date</td>
                    <td>:</td>
                    <td><?php
                            echo form_input(array("name" => "tanggal_invoice", "id" => "tanggal_invoice", "class" => "read-only", "readonly" => "readonly", "size" => "47"), $tanggal_invoice, "onchange=\"setInvoiceNumber()\"");
                            if ($nomor_invoice == "")
                                echo "<script>setInvoiceNumber()</script>";
                            ?>
                    </td>
                </tr>
                <tr>
                    <td colspan='9'>&nbsp;</td>
                </tr>
                <tr>
                    <td>To</td>
                    <td>:</td>
                    <td><?php echo $nama_customer; ?></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                </tr>
                <tr>
                    <td>Detail Address</td>
                    <td>:</td>
                    <td><?php echo $alamat_detail_customer; ?></td>
                    <td>&nbsp;</td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                    <td>Phone/Fax</td>
                    <td>:</td>
                    <td><?php echo $kontak_komplit; ?></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><?php echo $alamat_komplit; ?></td>

                </tr>
                <tr>
                    <td>Currency</td>
                    <td>:</td>
                    <td>
                            <?php
                            if ($using_dollar_currency == 1) {
                                $crc = "USD";
                            } else {
                                $crc = "IDR";
                            }
                            ?>
                        <select id="curency" name="crc" onchange="updateCrcSymbol()" value="<?php echo $crc; ?>">
                            <option value="<?php echo $crc; ?>"><?php echo $crc; ?></option>
                            <option value="USD">USD</option>
                            <option value="IDR">IDR</option>
                        </select>
                    </td>
                    <td>&nbsp;</td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                    <td>PPH</td>
                    <td>:</td>
                    <td><?php echo form_checkbox(array("name"=>"tambahkan_ppn","id" => "tambahkan_ppn"), 1, $tambahkan_ppn, "onclick=\"updateGrandTotal()\"");?>PPH</td>
                </tr>

                <tr>
                    <td>Diskon</td>
                    <td>:</td>
                    <td><?php echo form_input(array("name" => "diskon_iv", "id" => "diskon_iv", "size" => "50"), $diskon); ?></td>
                </tr>

                <tr>
                    <td colspan="9">
                        <br/><br/>

                        <br/>
                            <?php echo $gbitemdollar; ?>
                        <br/><br/>

                        <table class="GridTable" style="width: 100%;">
                            <thead>
                                <tr class="GridHeader">
                                    <td>No.</td>
                                    <td>Nama</td>
                                    <td>Jumlah</td>
                                    <td class="IDR">Harga Beli (Rp)</td>
                                    <td class="IDR">Harga Jual (Rp)</td>
                                    <td class="USD">Harga Beli ($)</td>
                                    <td class="USD">Harga Jual ($)</td>
                                    <td>Total</td>
                                    <td width="5%">Remove</td>
                                </tr>
                            </thead>
                            <tbody id="tablebody">
                                <tr class="GridItem1" id="no_item">
                                    <td colspan="8">Tidak ada data</td>
                                </tr>
                                    <?php
                                    $total_harga = 0;
                                    $totald_harga = 0;
                                    $nomor = 1;
                                    if (is_object($item)) {
                                        if ($item->num_rows() > 0) {
                                            $nomor = 1;
                                            foreach ($item->result() as $row) {
                                                echo
                                                '<tr class="GridItem1">' .
                                                        '<td>' . $nomor . '<input type="hidden" name="id_item[]" value="' . $row->id_item . '" /><input type="hidden" name="satuan[]" value="' . $row->satuan_beli . '" /></td>' .
                                                        '<td><input name="nama_item[]" type=text value="' . $row->nama_item . '" id=nama_item_' . $nomor . '></td>' .
                                                        '<td><input name="quantity[]" onblur="UpdateTotals(' . $nomor . ');" type=text value=' . $row->quantity . ' id=qty_' . $nomor . '></td>' .
                                                        '<td class="IDR" align="right"><input name="harga_beli[]" onblur="UpdateTotals(' . $nomor . ');" type=text value=' . $row->harga_beli . ' id=harga_beli_' . $nomor . '></td>' .
                                                        '<td class="IDR" align="right"><input name="harga_jual[]" onblur="UpdateTotals(' . $nomor . ');" type=text value=' . $row->harga_jual . ' id=harga_jual_' . $nomor . '></td>' .
                                                        '<td class="IDR" align="right"><span style="float: left;">Rp. </span><span class=total id=total_' . $nomor . '>' . ($row->harga_jual * $row->quantity) . '</span></td>' .
                                                        '<td class="USD" align="right"><input name="harga_beli_dollar[]" onblur="UpdateTotals(' . $nomor . ');" type=text value=' . $row->harga_beli_dollar . ' id=harga_beli_dollar_' . $nomor . '></td>' .
                                                        '<td class="USD" align="right"><input name="harga_jual_dollar[]" onblur="UpdateTotals(' . $nomor . ');" type=text value=' . $row->harga_jual_dollar . ' id=harga_jual_dollar_' . $nomor . '></td>' .
                                                        '<td class="USD" align="right"><span style="float: left;">$. </span><span class=total_dollar id=total_dollar_' . $nomor . '>' . ($row->harga_jual_dollar * $row->quantity) . '</span></td>' .
                                                        '<td><a class="delete" onclick="remove_element(' . $nomor . ',this);">&nbsp;</a></td>' .
                                                        '</tr>';
                                                $total_harga += $row->harga_jual * $row->quantity;
                                                $totald_harga += $row->harga_jual_dollar * $row->quantity;
                                                $nomor++;
                                            }
                                            echo "<script>jQuery(\"#no_item\").hide();</script>";
                                        }
                                    } 
                                    ?>

                            </tbody>
                            <tfoot>
                                <tr class="GridHeader">
                                    <td colspan="5" align="right">Total</td>
                                    <td class="IDR" align="right"><span style="float: left;">Rp. </span><span id="total" class="shou"><?php echo $total_harga ?></span><span id="total_show">0</span></td>
                                    <td class="IDR">&nbsp;</td>

                                    <td class="USD" align="right"><span style="float: left;">$ </span><span id="totald" class="shou"><?php echo $totald_harga ?></span><span id="totald_show">0</span></td>
                                    <td class="USD">&nbsp;</td>
                                </tr>
                                <tr class="GridHeader">
                                    <td colspan="5" align="right">PPH (1.2%)</td>
                                    <td class="IDR" align="right"><span style="float: left;">Rp. </span><span id="ppntotal" class="shou">0</span><span id="ppntotal_show">0</span></td>
                                    <td class="IDR">&nbsp;</td>


                                    <td class="USD" align="right"><span style="float: left;">$ </span><span id="ppntotald" class="shou">0</span><span id="ppntotald_show">0</span></td>
                                    <td class="USD">&nbsp;</td>
                                </tr>
                                <tr class="GridHeader">
                                    <td colspan="5" align="right">Grand Total</td>
                                    <td class="IDR" align="right"><span style="float: left;">Rp. </span><span id="grandtotal" class="shou">0</span><span id="grandtotal_show">0</span></td>
                                    <td class="IDR">&nbsp;</td>

                                    <td class="USD" align="right"><span style="float: left;">$ </span><span id="grandtotald" class="shou">0</span><span id="grandtotald_show">0</span></td>
                                    <td class="USD">&nbsp;</td>
                                </tr>
                            <script>updateCrcSymbol();
                                updateGrandTotal();</script>
                            </tfoot>
                        </table>
                        <br/>
                    </td>
                </tr>
                <tr>
                    <td>Penerbit Invoice</td>
                    <td>:</td>
                    <td><?php echo form_input(array("name" => "penerbit_invoice", "id" => "penerbit_invoice", "size" => "50"), $penerbit_invoice); ?></td>
                    <td>&nbsp;&nbsp;&nbsp;</td>
                </tr>
            </table>
            <br class="bersih" />
            <div id="box-control-save">
                    <?= form_hidden("temp_id_iv", $temp_iv_id); ?>
                    <?= form_submit(array("name" => "btnSubmit", "class" => "CommandButton"), "Save") ?>&nbsp;
                    <?= form_button(array("class" => "CommandButton"), "Cancel", "onclick='close_gb();'") ?>
            </div>
                <?= form_close() ?>

        </div>
    </div>
</div>

    <?php } else { ?>
<script type='text/javascript'>
    function close_gb() {
    <?php echo CloseGB(true); // close greybox    ?>
    }
</script>

<div style="padding-top: 25%;">
    <div class="BoxMessage">Proses Berhasil, apakah anda mau mencetak invoice tersebut?<br /><?php echo $error; ?></div>

    <div id="box-control-save">
            <?= form_button(array("class" => "CommandButton"), "Ya", "onclick=\"window.open('" . site_url("transaksi_penyewaan_tc/invoice_print/$kode") . "', '_blank'); " . reloadParent() . "\"") ?>&nbsp;
            <?= form_button(array("class" => "CommandButton"), "Tidak", "onclick='close_gb();'") ?>
    </div>
</div>
    <?php } ?>

